package com.quiz.bis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quiz.bis.domain.model.ExamGroupMember;
import com.quiz.common.core.domain.R;
import com.quiz.bis.domain.vo.ExamGroupDetailVo;
import com.quiz.bis.mapper.ExamGroupDetailMapper;
import com.quiz.bis.mapper.ExamGroupMemberMapper;
import com.quiz.bis.service.IExamGroupDetailService;
import com.quiz.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ExamGroupDetailServiceImpl implements IExamGroupDetailService {
    private final ExamGroupDetailMapper detailMapper;
    private final ExamGroupMemberMapper memberMapper;

    @Override
    public R<ExamGroupDetailVo> getGroupDetail(Long groupId) {
        Long userId = getUserId();

        // 检查用户是否有权限访问该考试组
        boolean hasAccess = memberMapper.exists(
                new LambdaQueryWrapper<ExamGroupMember>()
                        .eq(ExamGroupMember::getExamGroupId, groupId)
                        .eq(ExamGroupMember::getUserId, userId)
                        .eq(ExamGroupMember::getDelFlag, 0)
        );

        if (!hasAccess) {
            throw new ServiceException("您没有权限查看该考试组");
        }

        ExamGroupDetailVo detail = detailMapper.selectDetailById(groupId, userId);
        return R.ok(detail);
    }

    private Long getUserId() {
        // 从SecurityUtils获取当前用户ID
        return 1L; // 实际项目中替换为真实获取方式
    }
}